package dryworks.jsf.parsers;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import dryworks.jsf.annotations.TagAnnotation;
import dryworks.jsf.examples.model.Example;

public class JSFAnnotatedExampleParser {
	private String commentStart = "<!--";

	private String commentEnd = "-->";

	private List<String> allLines = null;
	private Example example = null;
	private int index = 0;

	public Example parse(BufferedReader in) throws IOException {
		example = new Example();
		allLines = new ArrayList<String>();
		index = 0;
		List<TagAnnotation> annotations = parseRecursively(in);
		example.setAnnotations(annotations);

		StringBuffer source = new StringBuffer();
		for (String subline : allLines) {
			subline = cutOutAnnotationTag(subline);
			source.append(subline).append("\n");
		}
		example.setSourceWithRemovedAnnotations(source.toString());
		
		return example;
	}

	public List<TagAnnotation> parseRecursively(BufferedReader in)
			throws IOException {
		List<TagAnnotation> res = new ArrayList<TagAnnotation>();

		String line;
		while ((line = in.readLine()) != null) {
			allLines.add(line);
			index++;
			line = line.trim();
			if (line.startsWith(commentStart) && line.endsWith(commentEnd)) {
				String[] words = line.split(" ");
				String tagName = words[1];
				
				TagAnnotation tagAnnotation = new TagAnnotation();
				tagAnnotation.setName(tagName.replaceFirst("x:", ""));

				int p1 = line.indexOf(tagName) + tagName.length();
				int p2 = line.lastIndexOf(commentEnd);
				String text = line.substring(p1, p2);

				tagAnnotation.setText(text);

				res.add(tagAnnotation);

				if (tagName.equals("x:line")) {
					String nextLine = in.readLine();
					index++;

					allLines.add(nextLine);
					tagAnnotation.setContent(nextLine+"\n...\n");
				}

				if (tagName.equals("x:area")) {
					int startIndex = index;
					List<TagAnnotation> resFromArea = parseRecursively(in); // recursion
					tagAnnotation.setChildren(resFromArea);

					int endIndex = index;
					StringBuffer areaText = new StringBuffer();
					for (int i = startIndex; i < endIndex - 1; i++) {
						String subline = allLines.get(i);
						

						subline = cutOutAnnotationTag(subline);

						areaText.append(subline).append("\n");
					}
					tagAnnotation.setContent(areaText.toString());
				}
				if (tagName.equals("/x:area")) {
					return res;
				}
			}
		}

		return res;
	}

	public String cutOutAnnotationTag(String subline) {
		if (subline.indexOf(commentStart) != -1
				&& subline.indexOf("/x:area") != -1) {
			return "";
		}
		if (subline.indexOf(commentStart) != -1 && subline.indexOf("x:") != -1) {
			// remove annotation tag
			int pp1 = subline.indexOf("x:");
			String sublineHead = subline.substring(0, pp1);
			String sublineTail = subline.substring(pp1);
			int pp2 = sublineTail.indexOf(" ");
			sublineTail = sublineTail.substring(pp2);

			return sublineHead + sublineTail;
		}
		return subline;
	}

	public static void main(String[] args) {
		try {
			File file = new File("WebContent/componentsUsageExamples/outputText/ExampleOfOutputText.jspx");

			if (file.exists()) {
				System.out.println("exist");
			} else {
				System.out.println("not exist");
			}

			JSFAnnotatedExampleParser parser = new JSFAnnotatedExampleParser();
			BufferedReader rin = new BufferedReader(new InputStreamReader(
					new FileInputStream(file)));
			Example res = parser.parse(rin);
			System.out.println("" + res.getAnnotations());
			System.out.println("source:\n" + res.getSourceWithRemovedAnnotations());
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
